const app = getApp()
const state = [{
    name: "常用功能",
    id: 1,
    arr: [{
        name: "我的评价",
        img: "comment-o",
        type: 1
      },
      {
        name: "就诊人管理",
        img: "friends-o",
        type: 2
      },
      {
        name: "紧急联系人",
        img: "manager-o",
        type: 3
      },
      {
        name: "联系客服",
        img: "phone-o",
        type: 5
      },
      {
        name: "我的优惠券",
        img: "bag-o",
        type: 9,
      },
      {
        name: "实名认证",
        img: "contact-o",
        type: 10
      }
      /*   {
          name: "延期申请",
          img: "/assets/image/user/通知.png",
          type: 6
        }, */
    ],
    actions: [{
      name: ''
    }],
    showPhone: false
  },

]
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
import {
  ApiOrderDelayList,
  ApiOrderList,
  ApiOrderPay,
  ApiCancelPay,
  getCustomerPhone,
  setGetUserInfo
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    userInfo: {},
    state,
    num: 0
  },



  getUserInfo() {
    setGetUserInfo().then(res => {
      this.setData({
        userInfo: res.data,
      })
      wx.setStorageSync('userInfo', res.data)
    })
    /*   ApiOrderDelayList().then(res => {
      let val = 0
      res.data.forEach(item => {
        if (item.status == '0') {
          val += 1
        }
      })
      this.setData({
        num: val
      })
      console.log(val)
    })
 */

    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    }, () => {

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  /**
   * 生命周期函数--监听页面显示
   */

  onShow() {
    this.getUserInfo()
    if (typeof this.getTabBar === "function" &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },

  //投诉和建议

  complaintsSuggestions() {
    wx.navigateTo({
      url: '/pages/complaintsSuggestions/index',
    })
  },


  isRealName() {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo.card_no && userInfo.card_no.length > 0) {
      return true
    } else {
      wx.showModal({
        title: '提示',
        content: '您还未实名认证，是否立即前往？',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/realName/index?data=""',
            })
          }
        }
      })
      return false
    }
  },

  //跳转到开通会员的界面
  openMembership() {
    if (this.data.userInfo.card_no) {
      wx.navigateTo({
        /*  url: '/pages/memberShip/index', */
        url: '/pages/hospitalChoice/index',
        /*   hospitalChoice */
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请先实名认证',
        complete: (res) => {
          /*   if (res.cancel) {

            } */

          if (res.confirm) {
            wx.navigateTo({
              url: `/pages/realName/index?data=${JSON.stringify(this.data.userInfo)}`,
            })
          }
        }
      })
    }

  },


  //立即登录按钮
  reLogin() {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },


  onClickLeft() {
    wx.navigateTo({
      url: '/pages/user/visitorList/index',
    })
  },

  onClickRight() {
    wx.navigateTo({
      url: '/pages/user/satisfaction/list/index',
    })
  },

  onClickBottom(e) {
    const type = e.currentTarget.dataset.type
    switch (type) {
      case 1:
        wx.navigateTo({
          url: '/pages/myEvaluation/index',
        })
        break;
      case 2:
        if (this.isRealName()) {
          wx.navigateTo({
            url: '/pages/searchPatient/index',
          })
        }
        break;
      case 3:
        wx.navigateTo({
          url: '/pages/searchContact/index',
        })
        break;
      case 4:
        wx.navigateTo({
          url: '/pages/serviceRecord/index',
        })
        break;
      case 5:
        this.handleGetPhone()
        break;
      case 6:
        wx.navigateTo({
          url: '/pages/myExtension/index',
        })
        break;
      case 9:
        wx.navigateTo({
          url: '/pages/couponList/index',
        })
        break;
      case 10:
        wx.navigateTo({
          url: `/pages/realName/index?data=${JSON.stringify(this.data.userInfo)}`,
        })
        break;
      default:
        break;
    }
  },

  handleGetPhone(e) {
    const that = this
    getCustomerPhone().then(res => {
      that.onSelect(res.data)
    })
    /*   wx.showModal({
        title: "是否确认拨打电话？",
        success(res) {
          if (res.confirm) {
           

          }
        }

      }) */
    /*    const tel = '123456789'
       this.setData({
         showPhone: true,
         'actions[0].name': `${tel}`
       }) */
  },
  //拨号成功
  onSelect(event) {
    const tel = event
    wx.makePhoneCall({
      phoneNumber: tel,
      success: function () {
        console.log("拨号成功！")
      },
      fail: function () {
        console.log("拨号失败！")
      }
    })
  },
  onClose() {
    this.setData({
      showPhone: false
    });
  },
  onEdit() {
    const item = this.data.userInfo
    wx.navigateTo({
      url: '/pages/userEdite/index?item=' + JSON.stringify(item),
    })
  },


  onClickExit() {
    wx.clearStorage()
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})